<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
    
     <div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="formId">
					<div class="select-list">
						<ul>
							<li>
								请求方法：
								<select name="methodType" th:with="type=${@dict.getType('sys_apiconfig_method')}">
										<option value="">所有</option>
										<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
							    </select>
							</li>
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>
			
	        <div class="btn-group-sm" id="toolbar" role="group">
				<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:apiConfig:add">
					<i class="fa fa-plus"></i> 添加
				</a>
				<a class="btn btn-primary btn-edit disabled" onclick="$.operate.edit()" shiro:hasPermission="system:apiConfig:edit">
					<i class="fa fa-edit"></i> 修改
				</a>
				<a class="btn btn-danger btn-del btn-del disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:apiConfig:remove">
					<i class="fa fa-remove"></i> 删除
				</a>
				<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:apiConfig:export">
					<i class="fa fa-download"></i> 导出
				 </a>
				<a class="btn btn-info" onclick="refreshApi()" shiro:hasPermission="system:apiConfig:refresh">
					<i class="fa fa-refresh"></i> 刷新api
				</a>
			</div>
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true"></table>
			</div>
		</div>
	</div>
    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:apiConfig:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:apiConfig:remove')}]];
        var prefix = ctx + "system/apiConfig";
        //刷新api
        var refreshApi=function(){
            $.modal.confirm("确认要刷新所有api吗?", function() {
                var url = prefix+"/refresh";
                $.operate.submit(url, "get", "json", "");
            });
        };
        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
                modalName: "api配置",
		        showExport: true,
                columns: [{
		            checkbox: true
		        },
				{
					field : 'id', 
					title : 'id',
					visible: false
				},
				{
					field : 'serviceId', 
					title : '对应路由id',
				},
				{
					field : 'environment', 
					title : '环境',
                    formatter: function(value, item, index) {
                        if (item.environment === 1) {
                            return '<span class="label label-success">调试</span>';
                        }
                        else if (item.environment === 2) {
                            return '<span class="label label-primary">测试</span>';
                        }
                        else if (item.environment === 3) {
                            return '<span class="label label-warning">正式</span>';
                        } else if (item.environment === 4) {
                            return '<span class="label label-danger">维护</span>';
                        }
                    }
				},
				{
					field : 'methodType', 
					title : '请求方法',
                    formatter: function(value, item, index) {
                        if (item.methodType === 0) {
                            return '<span class="label label-success">ALL</span>';
                        }
                        else if (item.methodType === 1) {
                            return '<span class="label label-success">GET</span>';
                        }
                        else if (item.methodType === 2) {
                            return '<span class="label label-success">POST</span>';
                        }
                    }
				},
				{
					field : 'title', 
					title : '标题',
				},
				{
					field : 'desc', 
					title : '接口描述',
				},
				{
					field : 'validateParams', 
					title : '需要校验的字段',
				},
				{
					field : 'authType', 
					title : '权限配置',
                    formatter: function(value, item, index) {
                        if (item.authType === 0) {
                            return '<span class="label label-danger">不需要</span>';
                        } else{
                            return '<span class="label label-primary">需要</span>';
                        }
                    }
				},
				{
					field : 'patternUrl', 
					title : '接口uri'
				},
				{
					field : 'replenishRate', 
					title : '并发恢复量/s',
                    sortable: true
				},
				{
					field : 'burstCapacity', 
					title : '并发容量/s',
                    sortable: true
				},
				{
					field : 'state', 
					title : '状态',
                    formatter: function(value, row, index) {
                        return statusTools(row);
                    }
				},
				{
					field : 'createTime', 
					title : '创建时间',
                    sortable: true
				},
				{
					field : 'updateTime', 
					title : '修改时间',
                    sortable: true
				},
				{
					field : 'extra', 
					title : '额外参数',
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		            	var actions = [];
		            	actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
						return actions.join('');
		            }
		        }]
            };
            $.table.init(options);
        });
		/*api状态启停*/
        function statusTools(row) {
            if (row.state == 0) {
                return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.id + '\')"></i> ';
            } else {
                return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.id + '\')"></i> ';
            }
        }

        /* api启用 */
        function enable(id) {
            $.modal.confirm("确认要启用该api吗？", function() {
                $.operate.post(prefix + "/changeState", { "id": id, "state": 1 });
            })
        }

        /* api-停用 */
        function disable(id) {
            $.modal.confirm("确认要停用该api吗？", function() {
                $.operate.post(prefix + "/changeState", { "id": id, "state": 0 });
            })
        }
    </script>
</body>
</html>